﻿using System;
using System.Collections;
using System.Collections.Generic;
using Gameplay.PVE.Survivor;
using OWL.Rendering.HRP;
using UnityEngine;
using UnityEngine.Serialization;
using Yoozoo.HRP.Runtime;

public class MapObstacleData : MonoBehaviour
{
    public int bulletId;
    public List<MapObstacleState> states;

    public int hitEffect;
    public int hp;
    public int attack;
    public int defence;

    [FormerlySerializedAs("team")] public ETargetTeam targetTeam;

    public int selfTeam = 1;
    
    [Header("范围，仅显示用")]
    public float radius;

    public bool createEnvironmentUnit = true;
    
    //运行时
    private EnvironmentUnit unit;

    private Vector3 position;

    public void Init()
    {
        if (!createEnvironmentUnit)
        {
            return;
        }
        gameObject.SetLayerRecursively(BattleConstValue.SurvivorObstacleLayer);
        unit = UnitManager.Instance.CreateEnvironmentUnit(this);
        unit.SetCollider(GetComponent<Collider>());
        this.position = transform.position;
    }
    
    private void Update()
    {
        
    }

    public void OnHit(List<int> elements,BattleUnit source,Vector3 position,Vector3 direction,Bullet bullet)
    {
        if (unit == null)
        {
            return;
        }
        if (unit.isDead)
        {
            return;
        }

        if (hitEffect > 0)
        {
            BattleEffectManager.Instance.CreateEffectAutoDispose(hitEffect,position,-direction,0.5f);
        }
        for (int i = 0; i < elements.Count; i++)
        {
            unit.AddElement(elements[i],source,bullet);
        }
        /*currentHp -= damage;
        if (hitEffect > 0)
        {
            BattleEffectManager.Instance.CreateEffectAutoDispose(hitEffect,position,-direction,0.5f);
        }
        if (currentHp <= 0)
        {
            if (bulletId > 0)
            {
                var config = BattleConfigManager.Instance.GetBulletConfig(bulletId);
                var bullet = BulletManager.Instance.CreateBullet(config);
                if (bullet != null)
                {
                    bullet.Initialize(config,StageManager.Instance.GetEnvironmentSpawner(team),StageManager.Instance.environmentUnit);
                    bullet.SetStartPosition(transform.position);
                    bullet.SetTargetPosition(transform.position);
                    bullet.Start();
                }
            }
            //BattleDebug.LogError("爆了 " + bulletId);
            valid = false;
            if (destroyType)
            {
                destroyType.SetActive(true);
            }

            if (normalType)
            {
                normalType.SetActive(false);
            }

            var collider = GetComponent<Collider>();
            if (collider)
            {
                collider.enabled = false;
            }
        }
        else
        {
            //BattleDebug.LogError("没爆 " + currentHitTimes);
        }*/
    }
}
